/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import model.Contact;

/**
 *
 * @author hbriand
 * 
 * @class ContactListPanel
 * @desc The list of contacts from one group
 */
public class ContactListPanel extends JPanel{
    /**
	 * 
	 */
	private static final long serialVersionUID = 8802461020141327602L;
	private ArrayList<Contact> list;
    private JList contactList;
    private ContactListDelegate delegate;

    public ContactListPanel() {
        this.list = new ArrayList<Contact>();
        this.redraw();
    }

    public void setContactList(List<Contact> sortedContactList) {
        this.list = new ArrayList<Contact>(sortedContactList);
        this.redraw();
    }

    public void myHide() {
        this.setVisible(false);
    }

    public void redraw() {
        this.removeAll();
        this.setSize(266, 600);
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        this.setPreferredSize(new Dimension(266, 600));

        // Add button to add a contact group
        JButton addBtn = new JButton("Add Contact");
        addBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                delegate.addContact();
            }
        });
        this.add(addBtn);

        //Contact list
        contactList = new JList();
        contactList.setFixedCellHeight(20);
        contactList.setFixedCellWidth(233);
        contactList.setVisibleRowCount(20);
        @SuppressWarnings("serial")
		DefaultListModel lm = new DefaultListModel() {};
        System.out.println(this.list);
        for (Contact c : this.list)
        {
            System.out.println(c.fullName());
            lm.addElement(c.fullName());
        }
        contactList.setModel(lm);
        contactList.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent lse) {
                delegate.contactSelected(list.get(contactList.getSelectedIndex()));
                System.out.println("List selection clicked : "+list.get(contactList.getSelectedIndex()));
            }
        });
        JScrollPane jsp = new JScrollPane(contactList);
        this.add(jsp);


        //Action buttons
        JButton editBtn = new JButton("Edit");
        editBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                delegate.editContact(list.get(contactList.getSelectedIndex()));
            }
        });
        this.add(editBtn);

        JButton delBtn = new JButton("Delete");
        delBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                delegate.deleteContact(list.get(contactList.getSelectedIndex()));
            }
        });
        this.add(delBtn);
    }

    public void setDelegate(ContactListDelegate aThis) {
        this.delegate = aThis;
    }

    public boolean hasElements() {
        return this.list.size()>0;
    }

}
